package com.beiing.leafchartdemo.slice;



import com.beiing.leafchat.OutsideLineChart;
import com.beiing.leafchat.bean.Axis;
import com.beiing.leafchat.bean.AxisValue;
import com.beiing.leafchat.bean.Line;
import com.beiing.leafchat.bean.PointValue;
import com.beiing.leafchartdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class OutsideLineChartSlice extends AbilitySlice {
    OutsideLineChart outsideLineChart;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_outside_line_chart);
        outsideLineChart = (OutsideLineChart) findComponentById(ResourceTable.Id_outside_linechart);
        initLineChart();
    }

    private void initLineChart() {
        Axis axisX = new Axis(getAxisValuesX());
        axisX.setAxisColor(RgbPalette.parse("#00000000")).setTextColor(Color.DKGRAY.getValue()).setHasLines(true);
        Axis axisY = new Axis(getAxisValuesY());
        axisY.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(false).setShowText(true);
        outsideLineChart.setAxisX(axisX);
        outsideLineChart.setAxisY(axisY);

        outsideLineChart.setChartData(getFoldLine());

        outsideLineChart.showWithAnimation(1000);

    }

    private List<AxisValue> getAxisValuesX(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(i + "日");
            axisValues.add(value);
        }
        return axisValues;
    }

    private List<AxisValue> getAxisValuesY(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(String.valueOf(i * 10));
            axisValues.add(value);
        }
        return axisValues;
    }

    private Line getFoldLine(){
        List<PointValue> pointValues = new ArrayList<>();

        PointValue p = new PointValue();
        p.setX(0);
        p.setLabel(String.valueOf(90));
        p.setY(90 / 100f);
        pointValues.add(p);
        SecureRandom random = new SecureRandom();
        for (int i = 2; i <= 12; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX( (i - 1) / 11f);
            int var = (int) (random.nextFloat() * 100);
//            int var = 20;
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }


        Line line = new Line(pointValues);
        line.setLineColor(RgbPalette.parse("#33B5E5"))
                .setLineWidth(3)
                .setPointColor(Color.RED.getValue())
                .setPointRadius(3)
                .setHasPoints(true)
                .setFill(true)
                .setFillColor(RgbPalette.parse("#33B5E5"))
                .setHasLabels(true)
                .setLabelColor(RgbPalette.parse("#33B5E5"));
        return line;
    }

}
